const db = require("../core/DBhelper");
const moment = require("moment");
class RolesController {

    /**
* @api {post} sys/addRole 角色添加
* @apiVersion 1.0.0
* @apiName addRole
* @apiGroup Role模块
* @apiParam {String} rname 角色名称
* @apiParam {String} desc  角色描述
* @apiSuccess {Number} code 结果标识
* @apiSuccess {String} msg 结果说明
* @apiSuccessExample Success-Response:
* 
*  {
     code: 200,
     msg: "添加角色成功"
 }
*/
    async addRole(req, res, next) {
        let add_sql = "INSERT INTO roles (`rname`,`desc`,`createtime`)VALUES(?,?,?);";
        let add_params = [
            req.body.rname,
            req.body.desc,
            moment().format("YYYY-MM-DD hh:mm:ss")
        ]
        let result_add = await db.exec(add_sql, add_params);
        let isAdd = result_add && result_add.affectedRows >= 1;

        res.json({
            code: isAdd ? 200 : -200,
            msg: isAdd ? "添加角色成功" : "添加角色失败"
        })

    }

    /**
    * @api {post} sys/search 角色搜索
    * @apiVersion 1.0.0
    * @apiName addRole
    * @apiGroup Role模块
    * @apiSuccess {Number} code 结果标识
    * @apiSuccess {String} msg 结果说明
    * @apiSuccessExample Success-Response:
    * 
    *  {
         code: 200,
         data: [] 所有角色信息
     }
    */
    async search(req, res, next) {
        let sql = "SELECT *FROM `roles` WHERE `status`=1;"
        let result = await db.exec(sql, [])
        res.json({
            msg: "查询成功",
            code: 200,
            data: result
        })
    }
    

    async update(req, res, next) { }
    async delete(req, res, next) {

    }

    //根据用户查询,当前有哪些角色
    /**
    * @api {post} sys/getRoleByUser 查询用户的角色
    * @apiVersion 1.0.0
    * @apiName getRoleByUser
    * @apiGroup Role模块
    * @apiParam {String} uId 用户id
    * @apiSuccess {Number} code 结果标识
    * @apiSuccess {String} msg 结果说明 角色id列表 [1,2,3]
    * @apiSuccessExample Success-Response:
    * 
    *  {
        code: 200,
        msg: "查询成功",
        data:[1,2,3] //角色的id
    }
    */
    async getRoleByUser(req, res, next) {
        let prams = [
            req.body.uId
        ]
        let sql = "SELECT  rId FROM user_role WHERE uId=? AND `status`=1;"
        let result = await db.exec(sql, prams);
        res.json({
            msg: "查询成功",
            code: 200,
            data: result.reduce((pre, cur) => { pre.push(cur.rId); return pre }, [])    //  [1,2]
        })
    }


    /**
    * @api {post} sys/addRoleByUser 用户配置角色
    * @apiVersion 1.0.0
    * @apiName addRoleByUser
    * @apiGroup Role模块
    * @apiParam {String} uId 用户id
    * @apiParam {Array} roleList 角色id列表 
    * @apiSuccess {Number} code 结果标识
    * @apiSuccess {String} msg 结果说明  给某个用户添加角色  uid 1, rid [1, 2,]
    * @apiSuccessExample Success-Response:
    * 
    *  {
     code: 200,
     msg: "角色设置完成"
    }
    */
    async addRoleByUser(req, res, next) {
        let roleList = JSON.parse(req.body.roleList || '[]');
        let prams = [
            req.body.uId,
            req.body.roleList, //数组
        ]
        //1. 把当前用户下的所以角色删除
        let update_sql_All = " DELETE  FROM  `user_role` WHERE uId=? ;";
        await db.exec(update_sql_All, [req.body.uId])

        //2.遍历的去添加角色
        for (var i = 0; i < roleList.length; i++) {
            var rId = roleList;
            let sql_Add = "INSERT INTO user_role(`uId`,`rId`)VALUES(?,?);";
            await db.exec(sql_Add, [req.body.uId, rId]);
        }

        res.json({
            msg: "角色设置完成",
            code: 200
        })
    }
}


module.exports = new RolesController();